// 不做mysql , json
const Koa = require('koa')
const router = require('koa-router')();
const app = new Koa()
const homeData = require('./Data/homeData/homeData.json')
const ServerData=require('./Data/serverData/ServerData.json')
const classifyData=require('./Data/classifyData/classifyData.json')
const RecommandData=require('./Data/classifyData/recommandData.json')
const SearchData=require('./Data/searchData/searchData.json')
const cors = require('koa2-cors')
const Mock = require('mockjs')
const {Random} = require('mockjs')
const path=require('path')

app.use(require('koa-static')(path.join(__dirname) + '/public'))

app.use(cors({
    origin: function(ctx) { //设置允许来自指定域名请求
        // if (ctx.url === '/test') {
        return '*'; // 允许来自所有域名请求
        // }
        // return 'http://localhost:3000'; //只允许http://localhost:8080这个域名的请求
    },
    maxAge: 5, //指定本次预检请求的有效期，单位为秒。
    credentials: true, //是否允许发送Cookie
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], //设置所允许的HTTP请求方法
    allowHeaders: ['Content-Type', 'Authorization', 'Accept'], //设置服务器支持的所有头信息字段
    exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'] //设置获取其他自定义字段
}))
router.get('/home', async (ctx) => {
    ctx.response.body = {
        success: true,
        data: homeData
    }
})
router.get('/home/server', async (ctx) => {
    ctx.body = {
        success: true,
        data: ServerData
    }
})

router.get('/classify/classifylist', async (ctx) => {
    ctx.body = {
        success: true,
        data: classifyData.classifyList
    }
})

router.get('/classify', async (ctx) => {
    let data = Mock.mock({
        'classifySecondMenu|9':[{
            'data|5-10':[{
                'type':'@ctitle(2,4)',
                'submenu|1-9':[{
                    'type':'@ctitle(2,4)',
                    'imgSrc':Random.image('50x50')
                }]
            }]
        }]
    })
    ctx.body = {
        success: true,
        data: {
            recommend:RecommandData,
            classifySecondMenu:data
        }
    }
})

router.get('/classify/classifylistdata', async (ctx) => {
    let {limit=20,page,classify}=ctx.request.query
    console.log(limit,page)
    let data = Mock.mock({
        'classifylistdata|20':[{
            'title':'@ctitle(3,7)'+classify,
            'lable':'xxx排行榜第'+'@integer(1,10)'+'名',
            'imgSrc':Random.image('170x170'),
            'notdiscountPrice':'@float(9,1000,0,1)',
            'price':'@float(9,1000,0,1)',
            'isdiscount':'@boolean',
            'discountname':'满xx减'+'@integer(10,20)',
            'discountstate':'优惠剩余'+'@integer(1,7)'+'天',
            'enable':'@boolean',
            'id':'@id'
        }]
    })
    ctx.body = {
        success: true,
        data: data
    }
})

router.get('/search',async(ctx)=>{
    ctx.body={
        success:true,
        data:SearchData
    }
})

router.get('/search/searchresult/:key', async (ctx) => {
    let {key} =  ctx.params
    let data = Mock.mock({
        'CommodityData|20':[{
            'title':'@ctitle(3,7)'+key,
            'lable':'xxx排行榜第'+'@integer(1,10)'+'名',
            'imgSrc':Random.image('170x170'),
            'notdiscountPrice':'@float(9,1000,0,1)',
            'price':'@float(9,1000,0,1)',
            'isdiscount':'@boolean',
            'discountname':'满xx减'+'@integer(10,20)',
            'discountstate':'优惠剩余'+'@integer(1,7)'+'天',
            'enable':'@boolean',
            'id':'@id'
        }]
    })
    ctx.body = {
        success: true,
        data: data
    }
})

router.get('/detail/:id', async (ctx) => {
    let {id} =  ctx.params
    let data = Mock.mock({
        'goodsDetailData':{
            'title':'@ctitle(3,7)'+id,
            "store":"@ctitle(3,5)",
            "recommandList|3-5":["@ctitle(10,16)"],
            "PraisePersent":"@float(90,100,0,1)",
            'lable':'xxx排行榜第'+'@integer(1,10)'+'名',
            'imgList|3-6':[Random.image('170x170',"#86c2f3","@ctitle(2,4)")],
            'notdiscountPrice':'@float(9,1000,0,1)',
            'price':'@float(9,1000,0,1)',
            'Postage':'@integer(0,99)',
            'isdiscount':'@boolean',
            "goodsAttribute|2-4":[
                {
                    "name":"@cword(2,4)",
                    "list|4-9":["@cword(4,8)"]
                }
            ],
            "place|20":[
                {
                    "province":"@province",
                    "menu|20":[
                        {
                            "city":"@city",
                            "county|20":["@city"]
                        }
                    ],
                }
            ],
            "server|3-7":["@ctitle(4,9)"],
            // "evaluate|6-9":[{     // 评论
            //     "name":"@cname",
            //     "avatar":Random.image("30x30"),
            //     "evaluate":"@csentence(10,20)",
            //     "image":Random.image("70x70")
            // }],
            // "relatedGoods|12":[{       // 推荐商品
            //     "img":Random.image("80x80"),
            //     "title":"@ctitle(5,9)",
            //     'price':'@float(9,1000,0,1)',
            // }],
            "detailImg|9":[Random.image("350x700","#897764","商品信息")],
            'discountname':'满xx减'+'@integer(10,20)',
            'discountstate':'距结束'+'@integer(1,7)'+'天'+"@integer(1,24)"+"小时",
            'enable':'@boolean',
            'id':'@id'
        }
    })
    ctx.body = {
        success: true,
        data: data
    }
})

app
    .use(router.routes())
    .use(router.allowedMethods())
// 1. http服务
// 2. 简单的路由模块
// 3. cors
// 4. 返回数据

app.listen(9090, () => {
    console.log('server is running 9090')
})
